/*
 * Name.java
 * Class that keeps track of title, firstname
 * lastname and middlename
 */
import java.sql.ResultSet;

public class Name {
	private String title;
	private String firstName;
	private String middleName;
	private String lastName;
	private String suffix;

	public Name(String title, String first, String middle, String last) {
		this(title, first, middle, last, "");
	}

	public Name(String title, String first, String middle, String last,
			String suffix) {
		this.title = title;
		this.firstName = first;
		this.middleName = middle;
		this.lastName = last;
		this.suffix = suffix;
	}

	/**
	 * gets title i.e. Ms, Mr, Mrs, Sr, Jr
	 * 
	 * @return title in string format
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * sets title
	 * 
	 * @param title
	 *            in string format
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * gets first name
	 * 
	 * @return first name in string format
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * sets first name
	 * 
	 * @set first name in string format
	 */
	public void setFirstName(String name) {
		this.firstName = name;
	}

	/**
	 * gets middle name
	 * 
	 * @return middle name in string format
	 */
	public String getMiddleName() {
		return middleName;
	}

	/**
	 * sets middle name
	 * 
	 * @set middle name in string format
	 */
	public void setMiddleName(String name) {
		this.middleName = name;
	}

	/**
	 * get last name
	 * 
	 * @return last name in string format
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * sets last name
	 * 
	 * @param last
	 *            name in string format
	 */

	public void setLastName(String name) {
		this.lastName = name;
	}

	/**
	 * get suffix
	 * 
	 * @return suffix in string format
	 */
	public String getSuffix() {
		return lastName;
	}

	/**
	 * sets suffix
	 * 
	 * @param suffix
	 *            in string format
	 */

	public void setSuffix(String name) {
		this.suffix = name;
	}

	// CRUD functionality

	// Create
	public static boolean createName(String username, String title,
			String first, String middle, String last) {
		String sql = "INSERT INTO Name VALUES ('" + username + "', '" + title
				+ "', '" + first + "', '" + middle + "', '" + last + "')";
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// Read
	public static Name readName(String username) {
		String foundTitle = "";
		String foundFirst = "";
		String foundMiddle = "";
		String foundLast = "";
		String sql = "SELECT * FROM Name WHERE username='" + username + "'";
		ResultSet rs = Database.selectQuery(sql);
		// Should only be 1 record per username
		try {
			while (rs.next()) {
				foundTitle = rs.getString("title");
				foundFirst = rs.getString("first_name");
				foundMiddle = rs.getString("middle_name");
				foundLast = rs.getString("last_name");
			}
		} catch (Exception e) {
		}
		Name foundName = new Name(foundTitle, foundFirst, foundMiddle,
				foundLast);
		return foundName;
	}

	// Update
	public static boolean updateName(String username, String title,
			String first, String middle, String last) {
		String sql = "UPDATE Name SET title='" + title + "', first_name='"
				+ first + "', middle_name='" + middle + "', last_name='" + last
				+ "' WHERE username='" + username + "'";
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// Delete
	public static boolean deleteName(String username) {
		String sql = "DELETE FROM Name WHERE username='" + username + "'";
		int rows = Database.query(sql);
		if (rows == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
